home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / VGATUT2.ZIP / VGAFUNC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  4.7 KB  |  217 lines

  1. //////////////////////////////////
  2. // VGA Functions - Barny Mercer //
  3. //                //
  4. // Created : 1/8/95 @ 6:45pm    //
  5. //////////////////////////////////
  6.  
  7. #include "vgafunc.h"
  8. #include <memory.h>
  9. #include <math.h>
  10. #include <conio.h>
  11.  
  12. unsigned char *vga= (unsigned char *)0xA0000000;
  13.  
  14. //-------------------------------------------------
  15. // Clear entire video screen to specified colour
  16. //-------------------------------------------------
  17.  
  18. void Cls( unsigned char Colour ) { memset( vga, Colour, 64000 ); }
  19.  
  20. //---------------------------------
  21. // Change to specified video mode
  22. //---------------------------------
  23.  
  24. void VidMode( int Mode )
  25. {
  26.     _asm
  27.     {
  28.     mov ax, [Mode]
  29.     int 10h
  30.     }
  31. }
  32.  
  33. //------------------------------------------------------
  34. // PutPixel - Uses assembly code to place a pixel
  35. //          at X, Y directly to memory
  36. //------------------------------------------------------
  37.  
  38. void PutPixel( int X, int Y, unsigned char Colour )
  39. {
  40.       _asm
  41.       {
  42.       mov ax, 0xA000   ; point AX to video memory
  43.       mov es, ax       ; move segment pointer to ES
  44.                ; (actual pointer)
  45.       mov bx, [Y]
  46.       mov dx, bx       ; register to register is faster by 1 clock
  47.  
  48.       mov dh, dl       ; dx=y*256 + y
  49.       xor dl, dl       ; clear lower byte of DX
  50.  
  51.       shl bx, 6       ; bx=y*64
  52.       add bx, dx       ; bx=y*320
  53.  
  54.       add bx, [X]       ; bx=(y*320)+x
  55.       mov di, bx       ; move video pointer to correct place
  56.  
  57.       mov al, [Colour]
  58.       mov es:[di], al  ; move colour to memory
  59.       }
  60. }
  61.  
  62. //---------------------------------------------------
  63. // DrawLine - Draw a line from (X1, Y1)-(X2, Y2) in
  64. //          specified colour.
  65. //---------------------------------------------------
  66.  
  67. void FloatDrawLine( int X1, int Y1, int X2, int Y2, unsigned char Colour )
  68. {
  69.     float Slope, YPos, XPos;
  70.  
  71.     // swap values if necessary
  72.     if (X2<X1)
  73.     {
  74.     int Tmp = X2;
  75.     X2 = X1;
  76.     X1 = Tmp;
  77.  
  78.     Tmp = Y2;
  79.     Y2 = Y1;
  80.     Y1 = Tmp;
  81.     }
  82.  
  83.     if ( (Y2 != Y1) &&
  84.      (X2 != X1)
  85.        )
  86.     {
  87.     Slope = (float)(Y2-Y1)/(X2-X1); // gradient of line
  88.  
  89.     if (Slope<1)
  90.     {
  91.         YPos = Y1;
  92.  
  93.         for (int Temp=X1; Temp<=X2; Temp++)
  94.         {
  95.         PutPixel( Temp, (int)YPos, Colour );
  96.         YPos += Slope;
  97.         }
  98.     }
  99.     else
  100.     {
  101.         XPos = X1;
  102.  
  103.         for (int Temp=Y1; Temp<=Y2; Temp++)
  104.         {
  105.         PutPixel( (int)XPos, Temp, Colour );
  106.         XPos += (1/Slope);
  107.         }
  108.     }
  109.     }
  110.     else   // must be horizonal or verticle
  111.     {
  112.     if (X1 == X2)    // verticle
  113.     {
  114.         for (int Temp=Y1; Temp<=Y2; Temp++)
  115.         PutPixel( X1, Temp, Colour );
  116.     }
  117.     else    // horizontal
  118.     {
  119.         for (int Temp=X1; Temp<=X2; Temp++)
  120.         PutPixel( Temp, Y1, Colour );
  121.     }
  122.     }
  123.  
  124. }
  125.  
  126. //------------------------------------------------------------
  127. // BresDrawLine - Draw a line from (X1, Y1)-(X2, Y2) in
  128. //          specified colour using Bresenham's Algorithm
  129. //          Note : we also use a different pixel method
  130. //             here to make things easier.
  131. //------------------------------------------------------------
  132.  
  133. void BresDrawLine( int X1, int Y1, int X2, int Y2, unsigned char Colour )
  134. {
  135.     int XScope, YScope;
  136.     int XDir = 1;
  137.     int YDir = 320;
  138.  
  139.     int LinearDeviance = 0; // deviance of pixel line from mathematical line
  140.  
  141.     unsigned int Offset = 0;     // must be unsigned for range
  142.  
  143.     int Length = 0;
  144.  
  145.     XScope = (X2 - X1);
  146.     YScope = (Y2 - Y1);
  147.  
  148.     if (XScope < 0)
  149.     {
  150.     XScope =- XScope;   // get absolute value of X term
  151.     XDir = -1;
  152.     }
  153.  
  154.     if (YScope < 0)
  155.     {
  156.     YScope =- YScope;   // get absoulte value of Y term
  157.     YDir = -320;
  158.     }
  159.  
  160.     Offset = X1 + (Y1*320); // video offset of first point
  161.     LinearDeviance = 0;
  162.  
  163.     if (XScope > YScope)   // then begin incrementing on X
  164.     {
  165.     Length = XScope+1;
  166.  
  167.     for (int Tmp=0; Tmp <= Length; Tmp++)
  168.     {
  169.         vga[Offset] = Colour;    // put pixel
  170.  
  171.         LinearDeviance += YScope;
  172.  
  173.         if (LinearDeviance >= XScope)    // error threshold has been exceeded
  174.         {
  175.         LinearDeviance -= XScope;
  176.         Offset += YDir;
  177.         }
  178.  
  179.         Offset += XDir;
  180.     }
  181.     }
  182.     else        // begin incrementing on Y
  183.     {
  184.     Length = YScope+1;
  185.  
  186.     for (int Tmp=0; Tmp <= Length; Tmp++)
  187.     {
  188.         vga[Offset] = Colour;    // put pixel
  189.  
  190.         LinearDeviance += XScope;
  191.  
  192.         if (LinearDeviance >= YScope)    // error threshold has been exceeded
  193.         {
  194.         LinearDeviance -= YScope;
  195.         Offset += XDir;
  196.         }
  197.  
  198.         Offset += YDir;
  199.     }
  200.     }
  201. }
  202.  
  203. //------------------------------------------------------------
  204. // Round - Rounds a value of type float to the nearest integer
  205. //------------------------------------------------------------
  206.  
  207. int Round( float Value )
  208. {
  209.     if ( abs( (long)(Value - (int)Value)) < 0.5 )
  210.     if (Value > 0)
  211.         return ( (int)(Value+0.5) );
  212.     else
  213.         return ( (int)(Value-0.5) );
  214.  
  215.     return ( (int)Value );
  216. }
  217.